package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.PageKit;
import com.ruoyi.system.domain.IndustryEntity;
import com.ruoyi.system.mapper.IndustryMapper;
import com.ruoyi.system.service.IndustryService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("industryService")
public class IndustryServiceImpl extends ServiceImpl<IndustryMapper, IndustryEntity> implements IndustryService {

	@Override
	public List<Map<String, Object>> getIndustryData() {
		QueryWrapper<IndustryEntity> qw = new QueryWrapper<IndustryEntity>();
		qw.eq("parent_id", 0);
		qw.orderByAsc("sort");
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		List<IndustryEntity> list = this.list(qw);
		for (IndustryEntity industryEntity : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("label", industryEntity.getName());
			map.put("value", industryEntity.getId());
			map.put("sort", industryEntity.getSort());
			map.put("children", this.getChild(industryEntity.getId()));
			result.add(map);
		}
		return result;
	}
	
	public List<Map<String, Object>> getChild(String id){
		QueryWrapper<IndustryEntity> qw = new QueryWrapper<IndustryEntity>();
		qw.eq("parent_id", id);
		qw.orderByAsc("sort");
		List<IndustryEntity> list = this.list(qw);
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		for (IndustryEntity industryEntity : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("label", industryEntity.getName());
			map.put("value", industryEntity.getId());
			map.put("sort", industryEntity.getSort());
			map.put("children", this.getChildren(industryEntity.getId()));
			result.add(map);
		}
		return result;
	}
	
	public List<Map<String, Object>> getChildren(String id){
		QueryWrapper<IndustryEntity> qw = new QueryWrapper<IndustryEntity>();
		qw.eq("parent_id", id);
		qw.orderByAsc("sort");
		List<IndustryEntity> list = this.list(qw);
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		for (IndustryEntity industryEntity : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("label", industryEntity.getName());
			map.put("value", industryEntity.getId());
			map.put("sort", industryEntity.getSort());
			result.add(map);
		}
		return result;
	}

	public List<IndustryEntity> getIndustryById(String id) {
		QueryWrapper<IndustryEntity> qw = new QueryWrapper<IndustryEntity>();
		qw.eq("parent_id",id);
		qw.orderByAsc("sort");
		return this.list(qw);
	}

	public IPage<IndustryEntity> getIndustryAllData(Map<String,Object> params) {
		QueryWrapper<IndustryEntity> qw = new QueryWrapper<IndustryEntity>();
		qw.orderByAsc("sort");
		return  this.page(new Page<>(PageKit.getPage(params), PageKit.getSize(params), true), qw);
	}

}